package ass.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ass.beans.UserBean;
import ass.dao.support.UserDAOImpl;

/**
 * Servlet implementation class Verify
 */
public class Verify extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Verify() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = (String) request.getParameter("id");
		String username = (String) request.getParameter("username");
		UserDAOImpl userDAOImpl = new UserDAOImpl();
		
		UserBean userBean = userDAOImpl.getUser(username);
		if (userBean.getVerify_code().equals(id)){
			userBean.setActive("Y");
			userDAOImpl.update(username, userBean);
			String nextJSP = "";
			nextJSP = "/userEditProfile.jsp";
			HttpSession session = request.getSession(true);
			session.setMaxInactiveInterval(-1);
			session.setAttribute("username", username);
			request.setAttribute("invalid_password",  "<font color='green'>Verification success</font>");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(request, response);
		}else {
			response.setContentType("text/html");// from response, set content type
			PrintWriter out = response.getWriter();// from response, get output writer
			out.write("Activation failed");
		}
		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
